app.controller("z-hrRaApproval-c", [ '$scope','$rootScope','$z-http','$sce','z-hrRaApproval-s','z-raApproval-s','z-attendance-s','z-dictionary-s','z-corporation-s','z-department-s','z-role-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,raApprovalService,AttendanceService,DictionaryService,CorporationService,DepartmentService,RoleService,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";		  
		$scope.viewModel.approvalState=""         //审批状态
		$scope.viewModel.checkAll=false;          //全选状态
		//保存复选框状态
		$scope.viewModel.checkboxDataList=[];
		//下拉多选框长度
		$scope.multiSelectMaxHeight=300;
		$scope.editModel= {};
		$scope.editModel.title="";
		$scope.editModel.approvalStateList=[];
		$scope.viewModel.approvalTypeList = [];
		var now = new Date();                     //当前日期     
		var nowMonth = now.getMonth();            //当前月     
		var nowYear=$filter('date')(now,'yyyy');  //当前年
		//获取当月开始日期
		var monthStartDate = $filter("date")(new Date(nowYear, nowMonth, 1), "yyyy/MM/dd");
		//获取当前日期
        var dayEndDate= $filter("date")(now, "yyyy/MM/dd");
		$scope.viewModel.retroactiveStratDate=new Date(monthStartDate);   //起止日期
		$scope.viewModel.retroactiveEndDate=new Date(dayEndDate);	  	  //终止日期
		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.startDateOpened = false;
		$scope.viewModel.endDateOpened = false;
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'hrRaApproval'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//起止日期选择
		$scope.viewModel.startDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			 $scope.viewModel.startDateOpened = !$scope.viewModel.startDateOpened;
		};
		//起止日期选择
		$scope.viewModel.endDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			 $scope.viewModel.endDateOpened = !$scope.viewModel.endDateOpened;
		};
		
		//加载审批状态数据
		$scope.viewModel.bindSelectData = function(){
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"10"
			},
			function(data){
				$scope.viewModel.approvalStateList= data;
				//遍历并判断是否为通过或拒绝
				for(var i=0;i<$scope.viewModel.approvalStateList.length;i++){
					if($scope.viewModel.approvalStateList[i].dicCd=='2' || $scope.viewModel.approvalStateList[i].dicCd=='3'){
						$scope.editModel.approvalStateList.push($scope.viewModel.approvalStateList[i]);
					}
				}
			},function(error) {
						
			});
		};
		$scope.viewModel.bindSelectData();
		
		//加载信息
		$scope.viewModel.bindSelectRegionTypeData = function(){
			//查询公司
			CorporationService.getCorporationList({
				fromDate:$scope.viewModel.retroactiveEndDate,
			},
			function(data){
				var site = [];  
		        $.each(data, function(index, values) {  
		            site.push({  
		                label : "("+values.corporationCode+")"+values.corporationName,  
		                value : values.corporationCode  
		            });  
		        });  
		        $('#hrappCorporation').multiselect('dataprovider',site);
			},function(error) {
				
			});
		};
		$scope.viewModel.bindSelectRegionTypeData();
		
	
		/*******使用为下拉带勾选控件*********/
		//员工级别
		$('#hrapplevel').multiselect({
			includeSelectAllOption: true,
			enableFiltering: true,
			delimiterText: '; ',
            maxHeight: $scope.multiSelectMaxHeight
        });
		//公司
		$('#hrappCorporation').multiselect({
			includeSelectAllOption: true,
			enableFiltering: true,
			delimiterText: '; ',
            maxHeight: $scope.multiSelectMaxHeight
        });
		//公司选中事件
		$("#hrappCorporation").change(function(){  
			var value=$(this).val();
		    var data={};
		    data.corporationCodeList=value;
		    data.fromDate = $scope.viewModel.retroactiveEndDate;
		    if(value==null){
		    	var site = [];  
		    	$('#hrappDepartmentName').multiselect('dataprovider',site);
		    	$scope.viewModel.departmentList=[];
		    }else{
		    	//查询部门
			    DepartmentService.getDepartmentListByCorporationCodeList(data,
				function(data){
			    	var site = [];  
			        $.each(data, function(index, values) {  
			            site.push({  
			                label : "("+values.departmentCode+")"+values.departmentName,  
			                value : values.departmentCode  
			            });  
			        });  
			        $('#hrappDepartmentName').multiselect('dataprovider',site);
				},function(error) {
					
				});
		    }
        });  
		
		//部门
		$('#hrappDepartmentName').multiselect({
			includeSelectAllOption: true,
			enableFiltering: true,
			delimiterText: '; ',
            maxHeight: $scope.multiSelectMaxHeight
        });
		
		//加载考勤状态数据
		$scope.viewModel.bindSelectData = function(){
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"1"
			},
			function(data){
				$scope.viewModel.attendanceStateData = data;
			},function(error) {
						
			});
			
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"2"
			},
			function(data){
				$scope.viewModel.isRepairedData = data;
			},function(error) {
						
			});
			
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"3"
			},
			function(data){
				$scope.viewModel.isApprovaledData = data;
			},function(error) {
						
			});
		};
		$scope.viewModel.bindSelectData();
		//加载数据
		$scope.viewModel.bindData = function(){
			if($scope.viewModel.retroactiveStratDate>$scope.viewModel.retroactiveEndDate){
				$rootScope.alertWarning("申请日前者必须小于等于后者。");
        		return;
        	}
			$scope.viewModel.employeeList=[];
			if($scope.viewModel.employeeNo!=null){
				var employeeNoData = $scope.viewModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
		    //获取基本信息
			Service.getRetroactiveListByHR({
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage,
				attendanceDateStart:$scope.viewModel.retroactiveStratDate,
				attendanceDateEnd:$scope.viewModel.retroactiveEndDate,
				hrApprovalState:$scope.viewModel.approvalState,
				corporationList:$scope.viewModel.corporationList,
				departmentList:$scope.viewModel.departmentList,
				employeeList:$scope.viewModel.employeeList,
				employeeLevelList:$scope.viewModel.employeeLevelList,
			},
			function(data){
				$scope.viewModel.dataList = data.retroctiveBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.addCheckbox();
				$scope.viewModel.loadDefaultcheckbox();
			    $scope.viewModel.JudgeCheckboxAll();
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//判断是否为全选
		$scope.viewModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 if($scope.viewModel.dataList[i].approvalState!=3){
					 if($scope.viewModel.dataList[i].checkbox==false){
						 type=false;
						 break;
					 }else{
						 type=true;
					 }
				 }
			 }
			 if(type){
				 $scope.viewModel.checkAll=true;
			 }else{
				 $scope.viewModel.checkAll=false;
			 }
		};
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		//新增
		$scope.viewModel.add = function(retroctive){
			if($scope.viewModel.checkboxDataList.length<=0){
				$rootScope.alertWarning("未选择申请审批信息！");
				return;
			}
			$scope.viewModel.load();
			$scope.editModel.title="补签驳回";
			$scope.editModel.raApproval={};
			$scope.editModel.hrRaApproval={};
			$scope.editModel.hrRaApproval.approvalState=3;
			//验证不能为空
			$scope.editForm.description.$dirty=false;
		};

		//验证  
		$scope.viewModel.requiredForm= function() {
            	if($scope.editModel.hrRaApproval.description == undefined || $scope.editModel.hrRaApproval.description == ""){
            		$scope.editForm.description.$dirty=true;
            		$scope.editForm.description.$invalid=true;
            		return true;
            	}else{
            		$scope.editForm.description.$dirty=false;
            		$scope.editForm.description.$invalid=false;
            		return false;
            	}
        };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.editModel.hrRaApproval.auth=5;
			$scope.editModel.hrRaApproval.requestIdList=[];
			for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
				var retroactiveId=$scope.viewModel.checkboxDataList[i].retroactiveId;
				$scope.editModel.hrRaApproval.requestIdList[i]=retroactiveId;
			}
			$scope.viewModel.isLoading = true;
			Service.saveRejectRetroactive($scope.editModel.hrRaApproval,
				function(data){
					$scope.viewModel.checkboxDataList=[];
					$scope.viewModel.unLoad();
					$scope.viewModel.bindData();
					$rootScope.alertInfo("驳回成功。");
	
					$scope.viewModel.isLoading = false;
				},function(error) {
					$rootScope.alertError(error.fault.message);
					$scope.viewModel.isLoading = false;
				});
		};
		
		/*****************选择框*****************/
		
		//设置默认复选框值
		$scope.viewModel.addCheckbox=function(){
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 $scope.viewModel.dataList[i].checkbox=false;
			 }
		};
		
		//处理加载复选框选择状态
		$scope.viewModel.loadDefaultcheckbox=function(){
			 for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
				 for(var j=0;j<$scope.viewModel.dataList.length;j++){
					 if($scope.viewModel.checkboxDataList[i].retroactiveId==$scope.viewModel.dataList[j].retroactiveId){
						 if($scope.viewModel.checkboxDataList[i].checkbox){
							 $scope.viewModel.dataList[j].checkbox=true;
						 }else{
							 $scope.viewModel.dataList[j].checkbox=false;
						 }
					 }
				 }
			 }
		};
		
		//判断是否为全选
		$scope.viewModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 if($scope.viewModel.dataList[i].approvalState!=3){
					 if($scope.viewModel.dataList[i].checkbox==false){
						 type=false;
						 break;
					 }else{
						 type=true;
					 }
				 }
			 }
			 if(type){
				 $scope.viewModel.checkAll=true;
			 }else{
				 $scope.viewModel.checkAll=false;
			 }
		};
		//全选事件
		$scope.viewModel.checkboxAll=function(){
			//判断是否为全选
			 if($scope.viewModel.checkAll==false){
				 angular.forEach($scope.viewModel.dataList, function(value, key) {
					 if(value.approvalState!=3 && value.isClosed==0){
						 value.checkbox=true;
						 var type=true;
						 for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
							 if($scope.viewModel.checkboxDataList[i].retroactiveId==value.retroactiveId){
								 type=false;
								 break;
							 }else{
								 type=true;
							 }
						 }
						 if(type){
							 $scope.viewModel.checkboxDataList.push(value);
						 }
					 }
				 });
			 }else{
				 angular.forEach($scope.viewModel.dataList, function(value, key) {
					 value.checkbox=false;
					 for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
						 if($scope.viewModel.checkboxDataList[i].retroactiveId==value.retroactiveId){
							 $scope.viewModel.checkboxDataList.splice(i,1);
						 }
					 }
				 });
			 }
		};
		//单选事件
		$scope.viewModel.checkbox=function(hrRaApproval){
			 if(hrRaApproval.checkbox){
				 hrRaApproval.checkbox=false;
				 for(var i=0;i<$scope.viewModel.checkboxDataList.length;i++){
					 if($scope.viewModel.checkboxDataList[i].retroactiveId==hrRaApproval.retroactiveId){
						 $scope.viewModel.checkboxDataList.splice(i,1);
					 }
				 }
			 }else{
				 hrRaApproval.checkbox=true;
				 $scope.viewModel.checkboxDataList.push(hrRaApproval);
			 }
			 $scope.viewModel.JudgeCheckboxAll();
		};
		
		//加载
		$scope.viewModel.loadSelects=function(){
			$scope.viewModel.showSelects=true;
		};
		//关闭加载
		$scope.viewModel.unLoadSelects=function(){
			$scope.viewModel.showSelects=false;
		};
		
		$scope.viewApprovalRecordModel = {};
		
		//查看补签状态过程
		$scope.viewModel.selectUsers = function(attendance){
			AttendanceService.getApprovalRecordByAttendanceId({
				attendanceId:attendance.attendanceId,
			},
			function(data){
				$scope.viewApprovalRecordModel.dataList = data;
				$scope.viewModel.isLoading=false;
				$scope.editModel.title="查看补签状态过程";
				$scope.viewModel.loadSelects();
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
} ]);